package com.gw.yunke.web;

import com.gw.yunke.domain.Comment;
import com.gw.yunke.domain.PraiseUserComment;
import com.gw.yunke.domain.Recomment;
import com.gw.yunke.domain.User;
import com.gw.yunke.domain.dto.AllCommentResp;
import com.gw.yunke.domain.dto.CommentReq;
import com.gw.yunke.domain.dto.CommentResp;
import com.gw.yunke.domain.dto.CreateCommentReq;
import com.gw.yunke.domain.dto.PraiseCommentReq;
import com.gw.yunke.domain.dto.ResultData;
import com.gw.yunke.service.ICommentService;
import com.gw.yunke.service.IUserService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 评论
 */
@RestController
@RequestMapping(value = "/comment")
public class CommentController {

  private static final Logger log = LoggerFactory.getLogger(CommentController.class);

  @Autowired
  private ICommentService commentService;
  @Autowired
  private IUserService userService;

  @ApiOperation(value = "创建评论", notes = "需登录状态 携带cookie", consumes = "application/x-www-form-urlencoded", response = ResultData.class)
  @ApiImplicitParams({
          @ApiImplicitParam(name = "content", required = true, value = "content 内容"),
          @ApiImplicitParam(name = "video_time", required = false, value = "video_time 评论在视频的第几分几秒 HH:mm:ss"),
          @ApiImplicitParam(name = "video_id", required = true, value = "视频id"),

  })
  @RequestMapping(value = "/create", method = RequestMethod.POST)
  public ResultData<String> createComment(@RequestBody CreateCommentReq createCommentReq,
                                          @CookieValue("sso") String token) throws Exception {


    User user = userService.getUserInfoByToken(token);
    if (user == null) {
      log.error("用户验证出错");
      throw new Exception("用户验证错误");
    }
    if (createCommentReq == null) {
      log.error("参数错误");
      throw new Exception("参数错误");
    }

    Comment comment = new Comment();
    comment.setContent(createCommentReq.getContent());
    comment.setCreateTime(new Date());
    comment.setUserId(user.getId());
    comment.setUserName(user.getName());
    comment.setVideoId(createCommentReq.getVideo_id());
//    if(commen != null) {
//      DateFormat format = new SimpleDateFormat("HH:mm:ss");
//      comment.setVideoTime(format.parse(video_time));
//    }
    int result = commentService.createComment(comment);
    ResultData<String> resultData = new ResultData<>();
    if (result > 0) {
      resultData.setCode(ResultData.OK);
      resultData.setMessage("ok");
      resultData.setData("发表成功");
    } else {
      resultData.setCode(ResultData.ERROR);
      resultData.setMessage("failed");
      resultData.setData("发表失败");
    }
    return resultData;
  }

  @ApiOperation(value = "回复评论", notes = "需登录状态 携带cookie", consumes = "application/x-www-form-urlencoded", response = ResultData.class)
  @ApiImplicitParams({
          @ApiImplicitParam(name = "content", required = true, value = "content 内容"),
          @ApiImplicitParam(name = "video_time", required = false, value = "video_time 评论在视频的第几分几秒 HH:mm:ss"),
          @ApiImplicitParam(name = "video_id", required = true, value = "视频id"),

  })
  @RequestMapping(value = "/re", method = RequestMethod.POST)
  public ResultData<String> createRecomment(@RequestParam(value = "comment_id", required = true) String comment_id,
                                            @RequestParam(value = "content", required = true) String content,
                                            @CookieValue("sso") String token) throws Exception {

    User user = userService.getUserInfoByToken(token);
    if (user == null) {
      log.error("用户验证出错");
      throw new Exception("用户验证错误");
    }
    Recomment recomment = new Recomment();
    recomment.setContent(content);
    recomment.setUserId(user.getId());
    recomment.setUserName(user.getName());

    int result = commentService.createRecomment(recomment);
    ResultData<String> resultData = new ResultData<>();
    if (result > 0) {
      resultData.setCode(ResultData.OK);
      resultData.setMessage("ok");
      resultData.setData("回复成功");
    } else {
      resultData.setCode(ResultData.ERROR);
      resultData.setMessage("failed");
      resultData.setData("回复失败");
    }
    return resultData;
  }


  @ApiOperation(value = "获取某个视频对应的所有评论", notes = "", response = ResultData.class)
  @ApiImplicitParams({
          @ApiImplicitParam(name = "video_id", required = true, value = "视频id")
  })
  @RequestMapping(value = "/get", method = RequestMethod.POST)
  public ResultData getComments(@RequestBody CommentReq commentReq, @CookieValue(required = false, value = "sso") String token) throws Exception {
    if (commentReq == null) {
      throw new Exception("参数错误");
    }
    Long userId = null;
    if (!StringUtils.isEmpty(token)) {
      User user = userService.getUserInfoByToken(token);
      if (user != null) {
        userId = user.getId();
      }
    }
    Long videoId = commentReq.video_id;
    List<CommentResp> commentRespList = commentService.getCommentsByVideoId(videoId, userId);
    ResultData<List<CommentResp>> resultData = new ResultData<>();
    resultData.setCode(ResultData.OK);
    resultData.setData(commentRespList);
    resultData.setMessage("ok");
    return resultData;
  }

  @ApiOperation(value = "获取某个视频对应的所有评论", notes = "", response = ResultData.class)
  @ApiImplicitParams({
          @ApiImplicitParam(name = "video_id", required = true, value = "视频id")
  })
  @RequestMapping(value = "/getHot", method = RequestMethod.POST)
  public ResultData getHotComments(@RequestBody CommentReq commentReq, @CookieValue(required = false, value = "sso") String token) throws Exception {
    if (commentReq == null) {
      throw new Exception("参数错误");
    }

    Long userId = null;
    if (!StringUtils.isEmpty(token)) {
      User user = userService.getUserInfoByToken(token);
      if (user != null) {
        userId = user.getId();
      }
    }

    Long videoId = commentReq.video_id;
    List<CommentResp> commentRespList = commentService.getHotCommentsByVideoId(videoId, userId);
    ResultData<List<CommentResp>> resultData = new ResultData<>();
    resultData.setCode(ResultData.OK);
    resultData.setData(commentRespList);
    resultData.setMessage("ok");
    return resultData;
  }


  @RequestMapping(value = "/praise", method = RequestMethod.POST)
  public ResultData<Integer> praise(@RequestBody PraiseCommentReq praiseCommentReq, @CookieValue("sso") String token) throws Exception {
    if (praiseCommentReq == null || praiseCommentReq.getCommentId() == null) {
      throw new Exception("未获取到参数信息");
    }
    User user = userService.getUserInfoByToken(token);
    if (user == null) {
      throw new Exception("未获取到登录信息");
    }

    List<PraiseUserComment> praiseUserCommentList = commentService.getCommentPraiseInfo(praiseCommentReq.getCommentId(),user.getId());
    if(praiseUserCommentList.size() > 0){
      throw new Exception("赞过了...");
    }
    praiseCommentReq.setUserId(user.getId());
    commentService.addCommentPraise(praiseCommentReq);
    Comment comment = commentService.getCommentById(praiseCommentReq.getCommentId());

    ResultData resultData = new ResultData();
    resultData.setCode(ResultData.OK);
    resultData.setData(comment.getPraise());
    return resultData;
  }

  @RequestMapping(value = "/getPraiseInfo", method = RequestMethod.POST)
  public ResultData<List<Long>> getUserPraise(@RequestBody CommentReq commentReq, @CookieValue("sso") String token) {
    ResultData resultData = new ResultData();
    try {
      User user = userService.getUserInfoByToken(token);
      if (user == null) throw new Exception("未找到用户");
      List<Long> commentIds = commentService.getUserPraiseInfo(commentReq.getVideo_id(), user.getId());

      resultData.setCode(ResultData.OK);
      resultData.setData(commentIds);
      return resultData;
    } catch (Exception e) {
      log.error("获取用户点赞信息错误 ", e);
      resultData.setCode(ResultData.ERROR);
      resultData.setMessage(e.getMessage());
      return resultData;
    }
  }

  @RequestMapping(value = "/getAll",method = RequestMethod.POST)
  public ResultData<AllCommentResp> getAllComment(@RequestBody CommentReq commentReq, @CookieValue(required = false, value = "sso") String token) throws Exception {
    if (commentReq == null) {
      throw new Exception("参数错误");
    }
    Long userId = null;
    if (!StringUtils.isEmpty(token)) {
      User user = userService.getUserInfoByToken(token);
      if (user != null) {
        userId = user.getId();
      }
    }
    Long videoId = commentReq.video_id;
    List<CommentResp> hotCommentRespList = commentService.getHotCommentsByVideoId(videoId, userId);
    List<CommentResp> commentRespList = commentService.getCommentsByVideoId(videoId, userId);

    AllCommentResp allCommentResp = new AllCommentResp();
    allCommentResp.setHotCommentRespList(hotCommentRespList);
    allCommentResp.setCommentRespList(commentRespList);

    ResultData<AllCommentResp> resultData = new ResultData<>();
    resultData.setCode(ResultData.OK);
    resultData.setData(allCommentResp);
    resultData.setMessage("ok");
    return resultData;
  }
}
